﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.Framework
{
    /// <summary>
    /// 程序内部结果类扩展
    /// </summary>
    public static class BaiTuInnerResultExtensions
    {
        /// <summary>
        /// 从Exception中获取内部传输结果类
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public static InnerResult GetInnerResult(this Exception ex, string errorMessage = "")
        {
            InnerResult innerResult = new InnerResult();
            if (ex != null)
            {
                innerResult.Success = false;
                if (!string.IsNullOrWhiteSpace(errorMessage))
                {
                    innerResult.ErrorMessage = string.Format("{0} \r\n {1}", errorMessage, ex.Message); ;
                }
                else
                {
                    innerResult.ErrorMessage = ex.Message;
                }
                innerResult.InnerException = ex;
            }
            else
            {
                innerResult.Success = true;
            }
            return innerResult;
        }
        /// <summary>
        /// 返回默认值
        /// </summary>
        /// <param name="innerResult"></param>
        /// <returns></returns>
        public static void Clear(this InnerResult innerResult)
        {
            innerResult.ErrorMessage = "";
            innerResult.InnerException = null;
            innerResult.Success = false;

        }
    }
}
